package cn.tannn.ops.nginx.entity;

import cn.tannn.ops.common.pojo.JpaCommonBean;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.joda.time.DateTime;
import org.joda.time.Duration;

import java.util.Date;

/**
 * 域名
 *
 * @author <a href="https://t.tannn.cn/">tan</a>
 * @version V1.0
 * @date 2024/8/22 下午2:46
 */
@Entity
@Table(name = "nginx_domain")
@Comment("域名")
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@DynamicUpdate
@DynamicInsert
public class Domains extends JpaCommonBean<Domains> {

    /**
     * 域名
     */
    @Comment("域名")
    @Column(columnDefinition = "varchar(100)  not null")
    String domain;

    /**
     * 颁发者
     */
    @Comment("颁发者")
    @Column(columnDefinition = "varchar(200)")
    String issuer;

    /**
     * subject
     */
    @Comment("subject")
    @Column(columnDefinition = "varchar(200)")
    String subject;

    /**
     * 剩余天数, 负数为过期 [。非数据库字段]
     */
    @Transient
    Long expiryDay;


    /**
     * 有效期结束时间
     */
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @Comment("有效期结束时间")
    @Column(columnDefinition = "timestamp")
    Date expiryAfter;

    /**
     * 有效期开始时间
     */
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @Comment("有效期开始时间")
    @Column(columnDefinition = "timestamp")
    Date expiryBefore;

    /**
     * 证书地址
     */
    @Comment("证书地址")
    @Column(columnDefinition = "varchar(200)")
    String sslPath;

    /**
     * webhook
     */
    @Comment("webhook")
    @Column(columnDefinition = "bigint")
    @JsonSerialize(using = ToStringSerializer.class)
    Long webhookId;

    /**
     * CloudAccessKey表里的accessId
     */
    @Comment("CloudAccessKey表里的accessId")
    @Column(columnDefinition = "varchar(100)")
    String accessId;


    /**
     * sshId[ssh_info表id]，根据这个自动更新线上过期的证书，sslPath不允许为空
     */
    @Comment("sshId[ssh_info表id]，根据这个自动更新线上过期的证书，sslPath不允许为空")
    @Column(columnDefinition = "bigint")
    @JsonSerialize(using = ToStringSerializer.class)
    Long sshId;

    /**
     * 是否有效。非数据库字段
     */
    @Transient
    boolean efficacious;

    public Long getExpiryDay() {
        DateTime begin = DateTime.now();
        DateTime end = new DateTime(expiryAfter);
        Duration etime = new Duration(begin, end);
        return etime.getStandardDays();
    }

    public boolean isEfficacious() {
        return getExpiryDay()>0;
    }

    public String pemPath(){
        if(sslPath.endsWith("/")){
            return sslPath+domain+".pem";
        }else {
            return sslPath+"/"+domain+".pem";
        }
    }

    public String keyPath(){
        if(sslPath.endsWith("/")){
            return sslPath+domain+".key";
        }else {
            return sslPath+"/"+domain+".key";
        }
    }
}
